package com.happy.xiao.common.rabbitmq.service.impl;

import com.happy.xiao.common.rabbitmq.service.ICommonMessage;
import com.happy.xiao.common.rabbitmq.service.IDLXMessage;
import com.happy.xiao.common.rabbitmq.service.IReliabilityMessage;
import com.happy.xiao.common.utils.Util;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service("DLXMessageService")
public class DLXMessageImpl implements IDLXMessage {

    @Autowired
    private Environment env;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    IReliabilityMessage iReliabilityMessage;

    // TODO ------------------------------------------------------------------------------------------------------------
    // TODO -------------------------------------------公共死信队列------------------------------------------------------
    // TODO ------------------------------------------------------------------------------------------------------------
    @Override
    public boolean sendCommonDelayMessage(String message, String callbackURL, long delayTimes) throws Exception {
        return createCommonDelay(message, callbackURL, delayTimes, 0);
    }

    @Override
    public <T> boolean sendCommonDelayMessage(T object, String callbackURL, long delayTimes) throws Exception {
        String message = Util.ObjectToJson(object);
        return createCommonDelay(message, callbackURL, delayTimes, 0);
    }

    @Override
    public boolean sendCommonDelayMessage(String message, String callbackURL, long delayTimes, long reliabilityTimes) throws Exception {
        return createCommonDelay(message, callbackURL, delayTimes, reliabilityTimes );
    }

    @Override
    public <T> boolean sendCommonDelayMessage(T object, String callbackURL, long delayTimes, long reliabilityTimes) throws Exception {
        String message = Util.ObjectToJson(object);
        return createCommonDelay(message, callbackURL, delayTimes, reliabilityTimes);
    }


    /**
     * 创建公共的死信消息
     *
     * @param message
     * @param callbackURL
     * @param delayTimes
     * @param reliabilityTimes
     * @return
     * @throws Exception
     */
    private boolean createCommonDelay(String message, String callbackURL, long delayTimes, long reliabilityTimes) throws Exception {
        String exchange = env.getProperty("common.produce.exchange.name");
        String routingKey = env.getProperty("common.produce.routing.key.name");
        return iReliabilityMessage.sendReliabilityDelayMessage(exchange, routingKey, message, callbackURL, delayTimes, reliabilityTimes);
    }

    // TODO ------------------------------------------------------------------------------------------------------------
    // TODO ------------------------------------------------------------------------------------------------------------
    // TODO ------------------------------------------------------------------------------------------------------------
}
